var express = require('express');
var router = express.Router();

//导入连个内置模块
const path = require('path')
const fs = require('fs')

var getimgData=require('../../utils/removeFilse')


var multer = require("multer");
var upload = multer({ dest: 'img_Upload/' }) //文件目录

router.post('/uploads', upload.single('files'), function (req, res, next) {
    let oldPath = req.file.path;
    let newPath = 'img_Upload/' + req.file.originalname;
    let oldUserImg = req.body.oldUserImg
    if (oldUserImg != '' || oldUserImg != null) {
        // 删除前端传来的文件
        getimgData(oldUserImg)
    } 

    fs.renameSync(oldPath, newPath)
    let CObj = {
        code: 200,
        msg: "上传成功",
        success: true,
        imgUrl: '/' + req.file.originalname
    }
    res.send(CObj)
});

module.exports = router;